package com.encryption.rpc;

import java.util.List;

import com.encryption.dao.Condition;
import com.encryption.dao.FileLogDAO;
import com.encryption.model.File;
import com.encryption.model.FileLog;
import com.encryption.model.User;
import com.encryption.rpc.model.WrapperPageInfo;
import com.encryption.service.FileService;
import com.encryption.service.UserService;
import com.encryption.service.impl.FileServiceImpl;
import com.encryption.service.impl.UserServiceImpl;
import com.sun.star.uno.Exception;

public class FileLogFacade {
	private FileService fileService = FileServiceImpl.getFileServiceImpl();
	private UserService userService = UserServiceImpl.getUserServiceImpl();

	public void addLog(FileLog log){
		FileLogDAO.getFileLogDAO().save(log);
	}
	
	public WrapperPageInfo getPagingLogByCond(List<Condition> condList, int page, int pageSize){
		List<FileLog> logList = FileLogDAO.getFileLogDAO().findPagingByCondition(condList, page, pageSize);
		for (FileLog log : logList){
			File file = fileService.getById(log.getFile() + "");
			User user = userService.getUserById(log.getUser());
			log.setFileObj(file);
			log.setUserObj(user);
		}
		
		int recordCount = getTotalCountByCond(condList);
		int allPageCount = (int) Math.ceil(((double)recordCount) / pageSize);
		WrapperPageInfo wraper = new WrapperPageInfo();
		wraper.setDataList(logList);
		wraper.setAllPageCount(allPageCount);
		wraper.setCurrentPage(page);
		wraper.setPageSize(pageSize);
		wraper.setRecordCount(recordCount);
		
		return wraper;
	}
	
	public FileLog getLogById(int id) throws Exception{
		FileLog log = FileLogDAO.getFileLogDAO().findById(id);
		if(log == null)
			throw new Exception("追踪失败");
		File file = fileService.getById(log.getFile() + "");
		User user = userService.getUserById(log.getUser());
		log.setFileObj(file);
		log.setUserObj(user);
		
		return log;
	}
	
	public int getTotalCountByCond(List<Condition> condList){
		return FileLogDAO.getFileLogDAO().findTotalCountByCondition(condList);
	}
}
